package com.zzh.myapplication.activity;

import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import net.icarplus.car.R;
import net.icarplus.car.constant.UrlValues;
import net.icarplus.car.manager.BaseActivity;
import net.icarplus.car.service.PublicRequest;
import net.icarplus.car.tools.Encrypt;
import net.icarplus.car.tools.SPUtils;
import net.icarplus.car.tools.T;
import net.icarplus.car.tools.ToolBarHelper;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2016/3/21.
 */
public class ChangeUserPasswdActivity extends BaseActivity {
    private Toolbar toolbar;
    private EditText et_user_old_passweod;
    private EditText et_user_new_passweod;
    private EditText et_user_new_agnin_passweod;
    private Button certificate_submit_btn;

    @Override
    public void setContent() {
        setContentView(R.layout.activity_change_user_passwd);
    }

    @Override
    public void initComponent() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        ToolBarHelper.setNormalTitleAndBack(this, toolbar, getString(R.string.password_revamp));
        et_user_old_passweod = (EditText) findViewById(R.id.et_user_old_passweod);
        et_user_new_passweod = (EditText) findViewById(R.id.et_user_new_passweod);
        et_user_new_agnin_passweod = (EditText) findViewById(R.id.et_user_new_agnin_passweod);
        certificate_submit_btn = (Button) findViewById(R.id.certificate_submit_btn);
    }

    String new_passweod;

    public void ChangeAcctPassToFrontParams() {
        String old_passwrod = et_user_old_passweod.getText().toString().trim();
        new_passweod = et_user_new_passweod.getText().toString().trim();

        String savePasswd = SPUtils.getString(ChangeUserPasswdActivity.this, "userPasswd", null);
        String oldPasswd = Encrypt.getMD5(old_passwrod);

        String agian_newPassword = et_user_new_agnin_passweod.getText().toString().trim();
        if (!savePasswd.equals(oldPasswd)) {
            T.show(ChangeUserPasswdActivity.this, "旧密码输入不正确，请从新输入", 1);
        }
        if (!new_passweod.equals(agian_newPassword)) {
            T.show(ChangeUserPasswdActivity.this, "两次输入不一致，请从新输入", 1);
        }
        if (new_passweod.equals(agian_newPassword) && savePasswd.equals(oldPasswd)) {
            JSONObject object = new JSONObject();
            String phone = SPUtils.getString(this, "LoginInfo", "");
            Log.e("----", "00000");
            try {
                object.put("mobile", phone);
                object.put("passwordType", "01");
                object.put("oldPassword", Encrypt.getMD5(old_passwrod));
                object.put("newPassword", Encrypt.getMD5(agian_newPassword));

            } catch (JSONException e) {
                e.printStackTrace();
            }
            PublicRequest.customMainRequestPost(this, UrlValues.CHANGE_ACCT_PASS_TO_FRONT, handler, object);
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 2000:
                    T.show(ChangeUserPasswdActivity.this, "修改成功", 1);
                    SPUtils.saveString(ChangeUserPasswdActivity.this, "userPasswd", Encrypt.getMD5(new_passweod));
                    finish();
                    break;
                case 3000:
                    T.show(ChangeUserPasswdActivity.this, "修改失败", 1);

                    break;
                case 1000:
                    T.show(ChangeUserPasswdActivity.this, "1000修改失败", 1);
                    break;
            }
        }
    };

    @Override
    public void setListener() {
        certificate_submit_btn.setOnClickListener(this);
    }

    @Override
    public void onAfterInit() {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.certificate_submit_btn:
                ChangeAcctPassToFrontParams();
                break;
        }
    }
}
